package main

import (
	//_ "mocker/routers"

	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"io/ioutil"
)

func main() {
	if beego.BConfig.RunMode == "dev" {
		beego.BConfig.WebConfig.DirectoryIndex = true
		beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
	}
	beego.Any("/*", func(context *context.Context) {
		firstLine := context.Request.Method + ":" + context.Request.URL.RequestURI()
		fmt.Println(firstLine)
		// header
		//context.Request.Header.Write(os.Stdout)
		var stringWriter = new(String)
		_ = context.Request.Header.Write(stringWriter)
		fmt.Println(stringWriter.value)
		body, err := ioutil.ReadAll(context.Request.Body)
		bodyStr := string(body)
		if err == nil {
			bodyStr := bodyStr
			fmt.Println("\n" + bodyStr)
		}
		var result = "server get content:\n" + firstLine + "\n" + stringWriter.value + "\n" + bodyStr
		_, _ = context.ResponseWriter.Write([]byte(result))
	})
	beego.Run()
}

type String struct {
	value string
}

// The only function required by the io.Writer interface.  Will append
// written data to the String.value string.
func (s *String) Write(p []byte) (n int, err error) {
	s.value += string(p)
	return len(p), nil
}
